package mapper

import (
	"github.com/beego/beego/v2/client/orm"
	"ksd-social-api/commons/page"
	"ksd-social-api/modules/special/model"
)

type SpecialBindMapper struct{}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 购买了专题
 */
func (mapper *SpecialBindMapper) GetSpecialBinds(bindId uint64, systemId uint64) (*model.SpecialBind, error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	var specialBinds *model.SpecialBind
	err := db.Raw(`SELECT * FROM xk_special_bind WHERE  system_id = ? AND id = ?`, systemId, bindId).QueryRow(&specialBinds)
	// 4: 结构返回
	return specialBinds, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 购买了专题
 */
func (mapper *SpecialBindMapper) FindAdminSpecialBinds(specialId uint64, systemId uint64) ([]*model.SpecialBind, error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	var specialBinds []*model.SpecialBind
	_, err := db.Raw(`SELECT * FROM xk_special_bind WHERE  system_id = ? AND special_id = ? and is_deleted = 0 and status = 1 order by sorted asc`, systemId, specialId).QueryRows(&specialBinds)
	// 4: 结构返回
	return specialBinds, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 购买了专题
 */
func (mapper *SpecialBindMapper) FindAdminSpecialBindsAll(specialId uint64, systemId uint64) ([]*model.SpecialBind, error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	var specialBinds []*model.SpecialBind
	_, err := db.Raw(`SELECT * FROM xk_special_bind WHERE  system_id = ? AND special_id = ? and is_deleted = 0  order by sorted asc`, systemId, specialId).QueryRows(&specialBinds)
	// 4: 结构返回
	return specialBinds, err
}

/**
 * 专题明细
 * @author feige
 * @date 2024-03-27
 * @version 1.0
 * @desc
 */
func (mapper *SpecialBindMapper) FindAdminSpecialBindDetails(specialId uint64, bindId uint64, systemId uint64) ([]*model.SpecialBindDetail, error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	var specialBindsDetails []*model.SpecialBindDetail
	// 3: 查询专题明细
	_, err := db.Raw(`SELECT * FROM xk_special_bind_detail WHERE  system_id = ? And bind_id = ? AND special_id = ? and is_deleted = 0 and status = 1 order by sorted asc`, systemId, bindId, specialId).QueryRows(&specialBindsDetails)
	// 4: 返回
	return specialBindsDetails, err
}

/**
 * 专题明细
 * @author feige
 * @date 2024-03-27
 * @version 1.0
 * @desc
 */
func (mapper *SpecialBindMapper) FindAdminSpecialBindDetailsAll(specialId uint64, bindId uint64, systemId uint64) ([]*model.SpecialBindDetail, error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	var specialBindsDetails []*model.SpecialBindDetail
	// 3: 查询专题明细
	_, err := db.Raw(`SELECT * FROM xk_special_bind_detail WHERE  system_id = ? And bind_id = ? AND special_id = ? and is_deleted = 0  order by sorted asc`, systemId, bindId, specialId).QueryRows(&specialBindsDetails)
	// 4: 返回
	return specialBindsDetails, err
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc 我学习的课程
 */
func (mapper *SpecialMapper) FindMySpecialBindsPage(userId uint64, systemId uint64, pageNo int64, pageSize int64) (p *page.Page, err error) {
	// 准备容器对象，开始装载数据库数据
	specialList := []model.Special{}
	//创建orm对象
	mysql := orm.NewOrm()

	var total int64 = 0
	// 开始执行sql查询
	err = mysql.Raw(`select count(1) from xk_special_bind_user_buy t2,xk_special_bind_detail t1 where t1.id = t2.opid and t1.status = 1 and t1.is_deleted = 0 and t2.user_id = ? and t2.system_id = ?`, userId, systemId).QueryRow(&total)
	if err != nil {
		return nil, err
	}
	// 重新换算分页和规则
	page := p.Page(pageNo, pageSize, total)
	// 执行count查询
	// 开始执行sql查询
	_, err2 := mysql.Raw(`
		select 
			t1.id,
			t1.title,
			t1.tags,
			t1.mode,
			t1.net_link,
			t1.pan_link,
			t1.pan_pwd,
			t1.try_link,
			t1.description,
			t1.viewcount,
			t1.download_nums,
			t1.create_time,
			t1.update_time,
			t1.comment_flag,
			t1.is_deleted,
			t1.avatar,
			t1.nickname,
			t1.status,
			t1.userid,
			t1.img,
			t1.fav_num,
			t1.comment_num,
			t1.price,
			t1.realprice,
			t1.new_flag,
			t1.hot_flag,
			t1.push_flag,
			t1.category_id,
			t1.category_name,
			t1.uuid,
			t1.like_num,
			t1.vip_flag,
			t1.buy_num
		from xk_special_bind_user_buy t2 ,xk_special_bind_detail t1 where t1.id = t2.opid and t1.status = 1 and t1.is_deleted = 0 and t2.user_id = ? and t2.system_id = ? order by t2.create_time desc limit ?,?`,
		userId, systemId, page.CurrentPage, page.PageSize).QueryRows(&specialList)

	// 6：把查询的数据放入到分页的records字段，准备返回
	page.Records = specialList
	// 7: 如果没有找到直接返回
	if err2 != nil {
		return nil, err2
	}
	return page, nil
}
